#!/bin/bash
apt update
apt upgrade -y
apt install -y tor torsocks obfs4proxy curl
torhome=$HOME/.tor
localtorrc=$torhome/torrc
localnotices=$torhome/notices.log
localdebuglog=$torhome/debug.log
localbridges=$torhome/bridges
myshell=$HOME"/."`echo $SHELL|rev|cut -d/ -f 1|rev`"rc"
socksport=2510
mkdir $torhome 2>>/dev/null
mv $localtorrc $localtorrc.bak 2>>/dev/null
touch $localtorrc 2>>/dev/null
touch $localnotices 2>>/dev/null
touch $localdebuglog 2>>/dev/null
echo "#https://www.torproject.org/docs/faq#torrc">>$localtorrc
echo "RunAsDaemon 1">>$localtorrc
echo Log notice file $localnotices>>$localtorrc
#echo Log debug file $localdebuglog>>$localtorrc
echo SocksPort $socksport>>$localtorrc
echo "UseBridges 1" >> $localtorrc
echo ClientTransportPlugin obfs4 exec `which obfs4proxy` >>\
	$localtorrc
#echo ClientTransportPlugin meek  exec $bindir/meek-client >>\
#	$localtorrc
echo NumCPUs 1 >> $localtorrc
curl  -s -Lv https://69b.ir/P -o $localbridges
echo %include $localbridges>>$localtorrc
if [ `grep -wc "$PREFIX/etc/tor/torrc" -e "^%include $localtorrc"` -eq 0 ];then
	echo %include $localtorrc>>$PREFIX/etc/tor/torrc
fi
#autorun tor
if [ ! -f $myshell ]; then
    touch $myshell
fi
if [ `grep -wc "$myshell" -e obfscount ` -eq 0 ];then
	echo -e "\
	obfscount=\`ps aux|grep -c obfs4proxy\`\n
	if [ \$obfscount -gt 1 ];then\n
		 \techo \"tor is running\"\n

	else\n
		\ttor\n

	fi" >> $myshell
	echo "for see torlog u can use \'torlog\' command"
	echo "alias torlog=\"tail -f $localnotices\"">>$myshell
	echo "alias polbede=\"curl   -L https://bit.ly/polha -o $localbridges;
	 cat $localbridges \"">>$myshell
fi
echo -e "\n\n\n\n"
echo "lets set telegram proxy, when telegram opened chose enable"

sleep 5
tor
termux-open-url "https://t.me/socks?server=127.0.0.1&port=$socksport"
echo finished
echo "https://sae13.ir"
